package weaver.interfaces.ybcn.duss.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.ybcn.duss.utils.T100Util;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2024-04-26
 * @Description: 询价单流程调用cwssp018采购单生成接口
 * @ModifyLog: OA-T100，OA将销售合同信息传给T100；
 **/
public class WeaXjdlcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaXjdlcAction>>>>>requestid=" + requestid);
        try {
            RecordSet rs = new RecordSet();
            T100Util t100Util = new T100Util();
            rs.execute("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            //供应商1数据
            rs.execute("select jsh,gyssl1,gys1x from "+tablename+" where mainid=? and gys1x is not null group by jsh,gyssl1,gys1x",mainid);
            if(rs.next()){
                String jsh = Util.null2String(rs.getString("jsh"));
                String gyssl1 = Util.null2String(rs.getString("gyssl1"));
                String gys1x = Util.null2String(rs.getString("gys1x"));
                JSONObject payloadJson = new JSONObject();
                JSONObject stddataJson = new JSONObject();
                JSONObject parameterJson = new JSONObject();
                JSONArray masterList = new JSONArray();
                JSONObject masterJson = new JSONObject();

                //单别	为空则取默认单别	物料类型
                masterJson.put("pmdldocno", Util.null2String(rs.getString("wllx")));
                //单据日期	为空则默认为 当天	归档日期
                masterJson.put("pmdldocdt", t100Util.getTimestamp());
                //采购人员	必填	采购员
                masterJson.put("pmdl002", Util.null2String(rs.getString("cgy")));
                //备注		采购理由
                masterJson.put("pmdl044", Util.null2String(rs.getString("cgly")));
                //采购性质	为空则默认 1 一般订单	寄售	jsh	"勾选：3 不勾选：1"
                masterJson.put("pmdl005", "".equals(jsh)?"1":"3");
                //税种	根据供应商带出	供应商税率
                masterJson.put("pmdl011", gyssl1);
                //来源单号	来记录对方的唯一key
                masterJson.put("source_no", requestid);
                JSONArray detailList = new JSONArray();
                //供应商1的数据
                int count = 1;
                rs.executeQuery("select id,wlbm,MEINS,slnew,EEIND,KM,bz,gys1jg,gyssl1,gys1x,gys1fkfsxx from "+tablename+"_dt1 where mainid=? and gys1x=?",mainid,gys1x);
                while (rs.next()){
                    JSONObject detailJson = new JSONObject();
                    String id = Util.null2String(rs.getString("id"));
                    String wlbm = Util.null2String(rs.getString("wlbm"));
                    String MEINS = Util.null2String(rs.getString("MEINS"));
                    String slnew = Util.null2String(rs.getString("slnew"));
                    String EEIND = Util.null2String(rs.getString("EEIND"));
                    String KM = Util.null2String(rs.getString("KM"));
                    String bz = Util.null2String(rs.getString("bz"));
                    String gys1jg = Util.null2String(rs.getString("gys1jg"));
                    String gyssl1_dt1 = Util.null2String(rs.getString("gyssl1"));
                    String gys1x_dt1 = Util.null2String(rs.getString("gys1x"));
                    String gys1fkfsxx = Util.null2String(rs.getString("gys1fkfsxx"));

                    //料号		物料编码
                    detailJson.put("pmdn001",wlbm);
                    //产品特征	为空则默认空格
                    detailJson.put("pmdn002","");
                    //采购单位		单位
                    detailJson.put("pmdn006",MEINS);
                    //采购数量		申请数量
                    detailJson.put("pmdn007",slnew);
                    //到库日期	为空则默认为出货日期+4天	需求日期
                    detailJson.put("pmdn014",t100Util.getTimestamp(EEIND));
                    //单价	不可<0	供应商价格
                    detailJson.put("pmdn015",gys1jg);
                    //税种	为空则默认为pmdl011	供应商税率
                    detailJson.put("pmdn016",gyssl1_dt1);
                    //理由码		预算科目
                    detailJson.put("pmdn049",KM);
                    //备注		备注	bz
                    detailJson.put("pmdn050",bz);
                    //来源项次	当对方的数据有项次之分时，用来记录对方的项次
                    detailJson.put("source_seq",count);
                    //供应商编号	必填	供应商
                    detailJson.put("pmdl004",gys1x_dt1);
                    //付款条件	供应商付款方式
                    detailJson.put("pmdl009",gys1fkfsxx);
                    detailList.add(detailJson);
                    count++;
                }

                masterJson.put("detail", detailList);
                masterList.add(masterJson);
                parameterJson.put("master", masterList);
                stddataJson.put("parameter", parameterJson);
                payloadJson.put("std_data", stddataJson);
                writeLog(">>>>>WeaXjdlcAction>>>>>payloadJson=" + payloadJson);
                JSONObject resultJson = t100Util.CPurchaseOrderCreate(payloadJson, "WeaXjdlcAction", requestid, src);
                writeLog(">>>>>WeaXjdlcAction>>>>>resultJson=" + resultJson);
                String message = Util.null2String(resultJson.getString("message"));
                if ("success".equals(message)) {
                    JSONObject dataJson = JSONObject.parseObject(resultJson.getString("data"));
                    JSONObject retPayloadJson = JSONObject.parseObject(dataJson.getString("payload"));
                    JSONObject retStddataJson = JSONObject.parseObject(retPayloadJson.getString("std_data"));
                    JSONObject retParameterJson = JSONObject.parseObject(retStddataJson.getString("parameter"));
                    int failnum = Util.getIntValue(retParameterJson.getString("failnum"), 0);
                    if (failnum > 0) {
                        JSONArray failreturnList = JSONArray.parseArray(retParameterJson.getString("fail_return"));
                        String errorMsg = failreturnList.getJSONObject(0).getString("error_msg");
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent("调用T100系统接口返回:" + errorMsg);
                        return Action.FAILURE_AND_CONTINUE;
                    } else {
                        JSONArray successreturnList = JSONArray.parseArray(retParameterJson.getString("success_return"));
                        String successMsg = successreturnList.getJSONObject(0).getString("success_msg");
                        rs.execute("update "+tablename+" set PARTNER='"+successMsg+"' where requestid=?",requestid);
                    }
                } else {
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent("调用T100系统接口返回:" + message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            //供应商2数据
            rs.execute("select jsh,gyssl2,gys2x from "+tablename+" where mainid=? and gys2x is not null group by jsh,gyssl2,gys1x",mainid);
            if(rs.next()){
                String jsh = Util.null2String(rs.getString("jsh"));
                String gyssl2 = Util.null2String(rs.getString("gyssl2"));
                String gys2x = Util.null2String(rs.getString("gys2x"));
                JSONObject payloadJson = new JSONObject();
                JSONObject stddataJson = new JSONObject();
                JSONObject parameterJson = new JSONObject();
                JSONArray masterList = new JSONArray();
                JSONObject masterJson = new JSONObject();

                //单别	为空则取默认单别	物料类型
                masterJson.put("pmdldocno", Util.null2String(rs.getString("wllx")));
                //单据日期	为空则默认为 当天	归档日期
                masterJson.put("pmdldocdt", t100Util.getTimestamp());
                //采购人员	必填	采购员
                masterJson.put("pmdl002", Util.null2String(rs.getString("cgy")));
                //备注		采购理由
                masterJson.put("pmdl044", Util.null2String(rs.getString("cgly")));
                //采购性质	为空则默认 1 一般订单	寄售	jsh	"勾选：3 不勾选：1"
                masterJson.put("pmdl005", "".equals(jsh)?"1":"3");
                //税种	根据供应商带出	供应商税率
                masterJson.put("pmdl011", gyssl2);
                //来源单号	来记录对方的唯一key
                masterJson.put("source_no", requestid);
                JSONArray detailList = new JSONArray();
                //供应商2的数据
                int count = 1;
                rs.executeQuery("select id,wlbm,MEINS,slnew,EEIND,KM,bz,gys2jg,gyssl2,gys2x,gys2fkfsxx from "+tablename+"_dt1 where mainid=? and gys2x=?",mainid,gys2x);
                while (rs.next()){
                    JSONObject detailJson = new JSONObject();
                    String id = Util.null2String(rs.getString("id"));
                    String wlbm = Util.null2String(rs.getString("wlbm"));
                    String MEINS = Util.null2String(rs.getString("MEINS"));
                    String slnew = Util.null2String(rs.getString("slnew"));
                    String EEIND = Util.null2String(rs.getString("EEIND"));
                    String KM = Util.null2String(rs.getString("KM"));
                    String bz = Util.null2String(rs.getString("bz"));
                    String gys2jg = Util.null2String(rs.getString("gys2jg"));
                    String gys2fkfsxx = Util.null2String(rs.getString("gys2fkfsxx"));
                    String gyssl2_dt1 = Util.null2String(rs.getString("gyssl2"));
                    String gys2x_dt1 = Util.null2String(rs.getString("gys2x"));
                    //料号		物料编码
                    detailJson.put("pmdn001",wlbm);
                    //产品特征	为空则默认空格
                    detailJson.put("pmdn002","");
                    //采购单位		单位
                    detailJson.put("pmdn006",MEINS);
                    //采购数量		申请数量
                    detailJson.put("pmdn007",slnew);
                    //到库日期	为空则默认为出货日期+4天	需求日期
                    detailJson.put("pmdn014",t100Util.getTimestamp(EEIND));
                    //单价	不可<0	供应商价格
                    detailJson.put("pmdn015",gys2jg);
                    //税种	为空则默认为pmdl011	供应商税率
                    detailJson.put("pmdn016",gyssl2_dt1);
                    //理由码		预算科目
                    detailJson.put("pmdn049",KM);
                    //备注		备注	bz
                    detailJson.put("pmdn050",bz);
                    //来源项次	当对方的数据有项次之分时，用来记录对方的项次
                    detailJson.put("source_seq",count);
                    //供应商编号	必填	供应商
                    detailJson.put("pmdl004",gys2x_dt1);
                    //付款条件	供应商付款方式
                    detailJson.put("pmdl009",gys2fkfsxx);
                    detailList.add(detailJson);
                    count++;
                }

                masterJson.put("detail", detailList);
                masterList.add(masterJson);
                parameterJson.put("master", masterList);
                stddataJson.put("parameter", parameterJson);
                payloadJson.put("std_data", stddataJson);
                writeLog(">>>>>WeaXjdlcAction>>>>>payloadJson=" + payloadJson);
                JSONObject resultJson = t100Util.CPurchaseOrderCreate(payloadJson, "WeaXjdlcAction", requestid, src);
                writeLog(">>>>>WeaXjdlcAction>>>>>resultJson=" + resultJson);
                String message = Util.null2String(resultJson.getString("message"));
                if ("success".equals(message)) {
                    JSONObject dataJson = JSONObject.parseObject(resultJson.getString("data"));
                    JSONObject retPayloadJson = JSONObject.parseObject(dataJson.getString("payload"));
                    JSONObject retStddataJson = JSONObject.parseObject(retPayloadJson.getString("std_data"));
                    JSONObject retParameterJson = JSONObject.parseObject(retStddataJson.getString("parameter"));
                    int failnum = Util.getIntValue(retParameterJson.getString("failnum"), 0);
                    if (failnum > 0) {
                        JSONArray failreturnList = JSONArray.parseArray(retParameterJson.getString("fail_return"));
                        String errorMsg = failreturnList.getJSONObject(0).getString("error_msg");
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent("调用T100系统接口返回:" + errorMsg);
                        return Action.FAILURE_AND_CONTINUE;
                    } else {
                        JSONArray successreturnList = JSONArray.parseArray(retParameterJson.getString("success_return"));
                        String successMsg = successreturnList.getJSONObject(0).getString("success_msg");
                        rs.execute("update "+tablename+" set PARTNER='"+successMsg+"' where requestid=?",requestid);
                    }
                } else {
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent("调用T100系统接口返回:" + message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaXjdlcAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}